package servlet;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javabean.Factory;
import javabean.book.Book;
import javabean.book.BookTag;
import javabean.user.User;

import javax.servlet.RequestDispatcher;

public class UserProfileServlet extends BaseServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8538076142868886263L;
	
	public void service() throws Exception{
		String uid = request.getParameter("uid");
		String suid = (String) request.getSession().getAttribute("uid");
		if(uid!=null && ! "".equals(uid)){
			List<User> all = null;
			List<Book> bAll = new ArrayList<Book>();
			List<Book> dAll = new ArrayList<Book>();
			List<Book> allBook = new ArrayList<Book>();
			List<User> usAll = null;
			List<BookTag> bookTag = new ArrayList<BookTag>();
			boolean isRole = false;
			try{
				all = Factory.getUserDAOInstance().queryAllByUidOrEmail(uid);	//其他用户
				bAll = Factory.getBookDAOInstance().queryAllBookByUid(uid);
				dAll = Factory.getBookDAOInstance().queryDebitBookByUid(suid);
				usAll = Factory.getUserDAOInstance().queryAllByUidOrEmail(suid);	//当前登陆用户
				allBook = Factory.getBookDAOInstance().queryAllBook();	//所有图书
				User user =  (User)usAll.get(0);
				if("1".equals(user.getRole())){
					isRole = true;
					bookTag = Factory.getBookDAOInstance().queryAllBookTag();
					request.setAttribute("bookTag", bookTag);
				}
			}catch(Exception ex){}
			if(all.size()>0){
				User user = (User)all.get(0);
				request.setAttribute("user", user);
			}
			request.setAttribute("userbook", bAll);//跳转
			request.setAttribute("dAll", dAll);
			request.setAttribute("isRole",isRole);
			request.setAttribute("allBook", allBook);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/user/profile.jsp");
			dispatcher.forward(request, response) ;
		}else{
			//跳转
			RequestDispatcher dispatcher = request.getRequestDispatcher("/");
			dispatcher.forward(request, response) ;
		}
	}
}
